'''
@ Project: news
@ File: user.py
@ Author: 韩成瑾
@ Date：2022/7/28 9:06

'''

from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse

from models.model import *
teacher_bp = Blueprint("teacher_bp",__name__,url_prefix="/teacher")


api = Api(teacher_bp)

# 教师视图
class Teacher_all(Resource):
    # 获取讲师列表
    def get(self):
        teacher_info = Teacher.query.limit(10).all()
        teacher_list = []
        print(teacher_info)
        for teacher in teacher_info:
            a = {
                'id':teacher.id,
                'name':teacher.name,
                'attention_num':teacher.attention_num,
                'info':teacher.info,
                'status':teacher.status,
                'photo':teacher.photo
            }
            teacher_list.append(a)
        return jsonify({
            'code':200,
            'teacher_list':teacher_list
        })

    # 添加讲师
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('attention_num')
        req.add_argument('phone')
        req.add_argument('password')
        req.add_argument('info')
        args = req.parse_args()

        teacher = Teacher(name=args['name'],attention_num=args['attention_num'],phone=args['phone'],password=args['password'],info=args['info'])
        db.session.add(teacher)
        db.session.commit()
        return jsonify({
            'code':200,'msg':'添加成功'
        })

# 教师视图
class Teacher_one(Resource):
    # 查询一个
    def get(self, id):
        teacher_one = Teacher.query.filter(Teacher.id == id).one()
        teacher_one_list = {
            'id': teacher_one.id,
            'name': teacher_one.name,
            'attention_num': teacher_one.attention_num,
            'info': teacher_one.info,
            'status': teacher_one.status,
            'photo': teacher_one.photo
        }
        return jsonify({
            'code':200,
            'teacher_one_list':teacher_one_list
        })

    # 修改教师
    def put(self,id):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('attention_num')
        req.add_argument('phone')
        req.add_argument('password')
        req.add_argument('info')
        args = req.parse_args()

        teacher_info = Teacher.query.filter(Teacher.id == id).first()
        if not teacher_info:
            return jsonify({
                'code':400,'msg':'没有这个老师'
            })
        Teacher.query.filter(Teacher.id == id).update(args)
        db.session.commit()
        return jsonify({
            'code':200,'msg':"修改成功"
        })

    # 删除教师
    def delete(self,id):
        req = reqparse.RequestParser()
        req.add_argument('id')
        args = req.parse_args()

        Teacher.query.filter(Teacher.id == id).delete()
        db.session.commit()

        return jsonify({
            'code':200,'msg':'删除成功'
        })














api.add_resource(Teacher_all, '/teacher')
api.add_resource(Teacher_one, '/teacher/<int:id>')


